<template>
    <div :class="rightNav ? 'w-[25%]' : 'w-[3%]'" class="bg-white/80 backdrop-blur-sm shadow-lg border-l border-gray-200/50 transition-all duration-300">
        <div v-if="rightNav" class="p-6 h-full flex flex-col">
            <div class="flex items-center justify-between mb-6">
                <h3 class="text-xl font-bold text-gray-900">题目列表</h3>
                <button class="p-2 hover:bg-gray-100 rounded-lg transition-colors duration-200" @click="handleToggleNav">
                    <svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                    </svg>
                </button>
            </div>

            <div class="flex-1 overflow-y-auto space-y-3">
                <div v-for="(topic, index) in topics" :key="topic.id" :class="[
                    'group p-4 rounded-xl cursor-pointer transition-all duration-300 border-2',
                    selectedTopic?.id === topic.id
                        ? 'bg-gradient-to-r from-indigo-500 to-purple-600 text-white border-indigo-500 shadow-lg transform scale-105'
                        : 'bg-white/60 hover:bg-white/80 border-gray-200 hover:border-indigo-300 hover:shadow-md'
                ]" @click="handleTopicSelect(topic.id)">
                    <div class="flex items-start justify-between mb-2">
                        <div class="flex-1 min-w-0">
                            <div class="font-semibold text-lg mb-2 line-clamp-2">
                                {{ topic.title }}
                            </div>
                            <div class="flex items-center gap-3 text-sm">
                                <span :class="[
                                    'px-3 py-1 rounded-full text-xs font-medium',
                                    selectedTopic?.id === topic.id
                                        ? 'bg-white/20 text-white'
                                        : getDifficultyClass(topic.difficulty)
                                ]">
                                    {{ topic.difficulty }}
                                </span>
                                <span :class="[
                                    'text-xs',
                                    selectedTopic?.id === topic.id ? 'text-white/80' : 'text-gray-500'
                                ]">
                                    {{ topic.submitter?.username || '未知用户' }}
                                </span>
                            </div>
                        </div>
                        <div :class="[
                            'ml-3 text-xs font-bold',
                            selectedTopic?.id === topic.id ? 'text-white' : 'text-gray-400'
                        ]">
                            #{{ index + 1 }}
                        </div>
                    </div>
                    
                    <div v-if="selectedTopic?.id === topic.id" class="mt-3 pt-3 border-t border-white/20">
                        <div class="flex items-center gap-2 text-sm">
                            <svg class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                            </svg>
                            <span>当前题目</span>
                        </div>
                    </div>
                </div>
                
                <div v-if="topics.length === 0" class="text-center py-12">
                    <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
                        <svg class="w-8 h-8 text-gray-400" viewBox="0 0 24 24" fill="none" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                        </svg>
                    </div>
                    <p class="text-gray-500 text-sm">暂无题目</p>
                </div>
            </div>
        </div>

        <div v-else class="flex flex-col items-center p-4 h-full justify-center">
            <button class="p-2 hover:bg-gray-100 rounded-lg transition-colors duration-200" @click="handleToggleNav">
                <svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v10a2 2 0 002 2h8a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
                </svg>
            </button>
        </div>
    </div>
</template>

<script setup lang="ts">
import type { TopicWithUser } from '~/types/topic'

interface Props {
    topics: TopicWithUser[]
    selectedTopic: TopicWithUser | null
    rightNav: boolean
}

interface Emits {
    (e: 'update:selectedTopic', topicId: number): void
    (e: 'update:rightNav', value: boolean): void
}
const { topics, selectedTopic, rightNav } = defineProps<Props>()

const emit = defineEmits<Emits>()

const handleTopicSelect = (topicId: number) => {
    emit('update:selectedTopic', topicId)
}

const handleToggleNav = () => {
    emit('update:rightNav', !rightNav)
}

const getDifficultyClass = (difficulty: string) => {
    switch (difficulty) {
        case '基础':
            return 'text-green-600 bg-green-100 px-2 py-1 rounded text-xs'
        case '中等':
            return 'text-yellow-600 bg-yellow-100 px-2 py-1 rounded text-xs'
        case '困难':
            return 'text-red-600 bg-red-100 px-2 py-1 rounded text-xs'
        default:
            return 'text-gray-600 bg-gray-100 px-2 py-1 rounded text-xs'
    }
}
</script>
